package org.example.dao.impl;

import org.example.dao.ItemDao;
import org.example.entity.Item;

import java.util.List;

public class ItemDaoImpl implements ItemDao {
    public void addItem(Item item){

        String sql = "INSERT INTO db_item (ord_id, pro_id, pro_name, quantity, subtotal) VALUES (?, ?, ?, ?, ?)";
      int row=DBUtil.excuteSql1(sql,item.getOrdId(),item.getProId(),item.getProName(),item.getQuantity(),item.getSubtotal());
      if(row>0){
          System.out.println("添加成功");
      }

    }
    public void delItem(int ordId){
        String sql="DELETE FROM db_item WHERE ord_id=?";
        int row=DBUtil.excuteSql1(sql,ordId);
        if(row>0){
            System.out.println("删除成功");
        }
    }
    public void updItem(Item item){
        String sql="UPDATE db_item SET ord_id=?,pro_id=?,pro_name=?,quantity=?,subtotal=? WHERE item_id=?";
        int row=DBUtil.excuteSql1(sql,item.getOrdId(),item.getProId(),item.getProName(),item.getQuantity(),item.getSubtotal(),item.getItemId());
        if(row>0){
            System.out.println("更新成功");
        }
    }
    public Item getProNameByOrderId(int ordId){
        String sql = "select * from db_item where ord_id=?";
        return DBUtil.executeQuery(Item::map, sql, ordId).get(0);
    }

    @Override
    public List<Item> getItemsByOrderId(int orderId) {
        String sql = "select * from db_item where ord_id=?";
        return DBUtil.executeQuery(Item::map, sql, orderId);
    }
 }
